/***************************************************************
Copyright ? wq Co., Ltd. 1998-2099. All rights reserved.
Name	    : app_Uart.c
Ver	        : V0.0
Des	        : user Uart app code
Else        : None
Log	        : V0.0 2023/12/20
***************************************************************/
#include "app_Uart.h"
#include "application.h"
#include "ringbuffer.h"

uint8_t read_character=0;
uint8_t bufs[5]={0xaa,0xbb,0xcc,0xdd,0xee};

Heartbeat protocol_live;
PTouch    protocol_touch;

uint8_t   RST_Count=0;
uint8_t   Live_Count=0;
uint8_t   Touch_State=0;

struct ring_buffer_t ring_buffer={{0},0,0};
uint8_t msg_recive_buffer[MAX_RX_BUFFER_LENGTH] = {0};
uint8_t msg_recive_buffer_old[MAX_RX_BUFFER_LENGTH] = {0};
/*  INT_A Function definition */
 CY_ISR(CySysUserUart_ISR);


/*******************************************************************************
* Function Name  : App_Uart_init(void)
* Description    : init uart
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void CySysUserUart_ISR()
{
    /* Returns the status/identity of which enabled RX interrupt source caused interrupt event */
    uint32 source = UART_Debug_GetRxInterruptSource();
	
	/* Checks for "RX FIFO AND not empty" interrupt */
    if(0ul != (UART_Debug_INTR_RX_NOT_EMPTY & source))
    {
        /* Get the character from terminal */
        read_character = UART_Debug_UartGetChar();
        ring_buffer_queue(&ring_buffer, (char) read_character);
        /* Clear UART "RX FIFO not empty interrupt" */
        UART_Debug_ClearRxInterruptSource(UART_Debug_INTR_RX_NOT_EMPTY);
    }
   /* Clear  interrupt Flag*/
    UART_Debug_ClearPendingInt();

}

/*******************************************************************************
* Function Name  : App_Uart_init(void)
* Description    : init uart
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void App_Uart_init(void)
{
      /*start UART operation*/
       UART_Debug_Start();
      /*enable interrupt handler for UART set user interrupt function*/
       UART_Debug_SCB_IRQ_StartEx(CySysUserUart_ISR);
      
      
}

/*******************************************************************************
* Function Name  : App_UART_Send
* Description    : uart send data function 
* Input          : databuff ,length
* Output         : None
* Return         : None
*******************************************************************************/
void App_UART_Send(uint8_t *databuff,uint8_t length)
{
    uint8_t i=0;
    for(i=0;i<length;i++)
    {
        UART_Debug_UartPutChar(databuff[i]);
    }
    
}

/*******************************************************************************
* Function Name  : App_CRC8_Check
* Description    : chech num 
* Input          : ptr:databuff ,len:length
* Output         : None
* Return         : None
*******************************************************************************/
uint8_t App_CRC8_Check(uint8_t *ptr, uint8_t len)
{
      uint32_t Crc_temp=0,i;
	  for(i=0;i<len;i++)
	  {
		  Crc_temp+=ptr[i];
	  }
	   Crc_temp=(~ Crc_temp)&0xff;
	  return  Crc_temp;
}
/*******************************************************************************
* Function Name  : App_message_analyse
* Description    : deal with message parse 
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void App_message_analyse()
{
    static uint8_t crc_data = 0;
    if(TRUE==CySysFlag.bits.recive_msg_flag)
    {
        crc_data=App_CRC8_Check(msg_recive_buffer,8);
        if(msg_recive_buffer[8]==crc_data)
          { 
                if(msg_recive_buffer[2]!=msg_recive_buffer_old[2])
                {
                         msg_recive_buffer_old[2]=msg_recive_buffer[2];
                        //LEDB2
                        if((msg_recive_buffer[2]&0x01)==0x01)
                        {CySysCMD.bits.LEDB2=1;}
                        else
                        {CySysCMD.bits.LEDB2=0;}
                        //LEDB1
                        if((msg_recive_buffer[2]&0x02)==0x02)
                        {CySysCMD.bits.LEDB1=1;}
                        else
                        {CySysCMD.bits.LEDB1=0;}
                        //LEDR5
                        if((msg_recive_buffer[2]&0x04)==0x04)
                        {CySysCMD.bits.LEDR5=1;}
                        else
                        {CySysCMD.bits.LEDR5=0;}
                        //LEDR4
                        if((msg_recive_buffer[2]&0x08)==0x08)
                        {CySysCMD.bits.LEDR4=1;}
                        else
                        {CySysCMD.bits.LEDR4=0;}
                         //LEDR3
                        if((msg_recive_buffer[2]&0x10)==0x10)
                        {CySysCMD.bits.LEDR3=1;}
                        else
                        {CySysCMD.bits.LEDR3=0;}
                        //LEDG1
                        if((msg_recive_buffer[2]&0x20)==0x20)
                        {CySysCMD.bits.LEDG1=1;}
                        else
                        {CySysCMD.bits.LEDG1=0;}
                        //LEDR2
                        if((msg_recive_buffer[2]&0x40)==0x40)
                        {CySysCMD.bits.LEDR2=1;}
                        else
                        {CySysCMD.bits.LEDR2=0;}
                        //LEDR1
                        if((msg_recive_buffer[2]&0x80)==0x80)
                        {CySysCMD.bits.LEDR1=1;}
                        else
                        {CySysCMD.bits.LEDR1=0;}
                }
                if(msg_recive_buffer[3]!=msg_recive_buffer_old[3])
                {
                    msg_recive_buffer_old[3]=msg_recive_buffer[3];
                    //LEDW6
                    if((msg_recive_buffer[3]&0x01)==0x01)
                    {CySysCMD.bits.LEDW6=1;}
                    else
                    {CySysCMD.bits.LEDW6=0;}
                    //LEDW5
                    if((msg_recive_buffer[3]&0x02)==0x02)
                    {CySysCMD.bits.LEDW5=1;}
                    else
                    {CySysCMD.bits.LEDW5=0;}
                    //LEDB3
                    if((msg_recive_buffer[3]&0x04)==0x04)
                    {CySysCMD.bits.LEDB3=1;}
                    else
                    {CySysCMD.bits.LEDB3=0;}
                    //LEDB4
                    if((msg_recive_buffer[3]&0x08)==0x08)
                    {CySysCMD.bits.LEDB4=1;}
                    else
                    {CySysCMD.bits.LEDB4=0;}
                    //LEDW4
                    if((msg_recive_buffer[3]&0x10)==0x10)
                    {CySysCMD.bits.LEDW4=1;}
                    else
                    {CySysCMD.bits.LEDW4=0;}
                    //LEDW3
                    if((msg_recive_buffer[3]&0x20)==0x20)
                    {CySysCMD.bits.LEDW3=1;}
                    else
                    {CySysCMD.bits.LEDW3=0;}
                    //LEDW2
                    if((msg_recive_buffer[3]&0x40)==0x40)
                    {CySysCMD.bits.LEDW2=1;}
                    else
                    {CySysCMD.bits.LEDW2=0;}
                    //LEDW1
                    if((msg_recive_buffer[3]&0x80)==0x80)
                    {CySysCMD.bits.LEDW1=1;}
                    else
                    {CySysCMD.bits.LEDW1=0;}
                }
                if(msg_recive_buffer[4]!=msg_recive_buffer_old[4])
                {
                    msg_recive_buffer_old[4]=msg_recive_buffer[4];
                    //Brightness bit0
                    if((msg_recive_buffer[4]&0x01)==0x01)
                    {CySysCMD.bits.BR0=1;}
                    else
                    {CySysCMD.bits.BR0=0;}
                     //Brightness bit1
                    if((msg_recive_buffer[4]&0x02)==0x02)
                    {CySysCMD.bits.BR1=1;}
                    else
                    {CySysCMD.bits.BR1=0;}
                     //Brightness bit2
                    if((msg_recive_buffer[4]&0x04)==0x04)
                    {CySysCMD.bits.BR2=1;}
                    else
                    {CySysCMD.bits.BR2=0;}
                    //Brightness bit3
                    if((msg_recive_buffer[4]&0x08)==0x08)
                    {CySysCMD.bits.BR3=1;}
                    else
                    {CySysCMD.bits.BR3=0;}
                    //LEDW9
                    if((msg_recive_buffer[4]&0x10)==0x10)
                    {CySysCMD.bits.LEDW9=1;}
                    else
                    {CySysCMD.bits.LEDW9=0;}
                    //LEDW8
                    if((msg_recive_buffer[4]&0x20)==0x20)
                    {CySysCMD.bits.LEDW8=1;}
                    else
                    {CySysCMD.bits.LEDW8=0;}
                    //LEDW7
                    if((msg_recive_buffer[4]&0x40)==0x40)
                    {CySysCMD.bits.LEDW7=1;}
                    else
                    {CySysCMD.bits.LEDW7=0;} 
                }
                if(msg_recive_buffer[5]!=msg_recive_buffer_old[5])
                {
                    msg_recive_buffer_old[5]=msg_recive_buffer[5];
                    //LEDWG1
                    if((msg_recive_buffer[5]&0x01)==0x01)
                    {CySysCMD.bits.LEDWG1=1;}
                    else
                    {CySysCMD.bits.LEDWG1=0;} 
                    //LEDWF1
                    if((msg_recive_buffer[5]&0x02)==0x02)
                    {CySysCMD.bits.LEDWF1=1;}
                    else
                    {CySysCMD.bits.LEDWF1=0;} 
                    //LEDWE1
                    if((msg_recive_buffer[5]&0x04)==0x04)
                    {CySysCMD.bits.LEDWE1=1;}
                    else
                    {CySysCMD.bits.LEDWE1=0;}
                    //LEDWD1
                    if((msg_recive_buffer[5]&0x08)==0x08)
                    {CySysCMD.bits.LEDWD1=1;}
                    else
                    {CySysCMD.bits.LEDWD1=0;}
                    //LEDWC1
                    if((msg_recive_buffer[5]&0x10)==0x10)
                    {CySysCMD.bits.LEDWC1=1;}
                    else
                    {CySysCMD.bits.LEDWC1=0;}
                    //LEDWB1
                    if((msg_recive_buffer[5]&0x20)==0x20)
                    {CySysCMD.bits.LEDWB1=1;}
                    else
                    {CySysCMD.bits.LEDWB1=0;}
                    //LEDWA1
                    if((msg_recive_buffer[5]&0x40)==0x40)
                    {CySysCMD.bits.LEDWA1=1;}
                    else
                    {CySysCMD.bits.LEDWA1=0;}
                
                }
                if(msg_recive_buffer[6]!=msg_recive_buffer_old[6])
                {
                   msg_recive_buffer_old[6]=msg_recive_buffer[6];
                    //LEDWG2
                    if((msg_recive_buffer[6]&0x01)==0x01)
                    {CySysCMD.bits.LEDWG2=1;}
                    else
                    {CySysCMD.bits.LEDWG2=0;} 
                    //LEDWF2
                    if((msg_recive_buffer[6]&0x02)==0x02)
                    {CySysCMD.bits.LEDWF2=1;}
                    else
                    {CySysCMD.bits.LEDWF2=0;} 
                    //LEDWE2
                    if((msg_recive_buffer[6]&0x04)==0x04)
                    {CySysCMD.bits.LEDWE2=1;}
                    else
                    {CySysCMD.bits.LEDWE2=0;}
                    //LEDWD2
                    if((msg_recive_buffer[6]&0x08)==0x08)
                    {CySysCMD.bits.LEDWD2=1;}
                    else
                    {CySysCMD.bits.LEDWD2=0;}
                    //LEDWC2
                    if((msg_recive_buffer[6]&0x10)==0x10)
                    {CySysCMD.bits.LEDWC2=1;}
                    else
                    {CySysCMD.bits.LEDWC2=0;}
                    //LEDWB2
                    if((msg_recive_buffer[6]&0x20)==0x20)
                    {CySysCMD.bits.LEDWB2=1;}
                    else
                    {CySysCMD.bits.LEDWB2=0;}
                    //LEDWA2
                    if((msg_recive_buffer[6]&0x40)==0x40)
                    {CySysCMD.bits.LEDWA2=1;}
                    else
                    {CySysCMD.bits.LEDWA2=0;}
                
                }
                if(msg_recive_buffer[7]!=msg_recive_buffer_old[7])
                {
                    msg_recive_buffer_old[7]=msg_recive_buffer[7];
                    //LEDWG3
                    if((msg_recive_buffer[7]&0x01)==0x01)
                    {CySysCMD.bits.LEDWG3=1;}
                    else
                    {CySysCMD.bits.LEDWG3=0;} 
                    //LEDWF3
                    if((msg_recive_buffer[7]&0x02)==0x02)
                    {CySysCMD.bits.LEDWF3=1;}
                    else
                    {CySysCMD.bits.LEDWF3=0;} 
                    //LEDWE3
                    if((msg_recive_buffer[7]&0x04)==0x04)
                    {CySysCMD.bits.LEDWE3=1;}
                    else
                    {CySysCMD.bits.LEDWE3=0;}
                    //LEDWD3
                    if((msg_recive_buffer[7]&0x08)==0x08)
                    {CySysCMD.bits.LEDWD3=1;}
                    else
                    {CySysCMD.bits.LEDWD3=0;}
                    //LEDWC3
                    if((msg_recive_buffer[7]&0x10)==0x10)
                    {CySysCMD.bits.LEDWC3=1;}
                    else
                    {CySysCMD.bits.LEDWC3=0;}
                    //LEDWB3
                    if((msg_recive_buffer[7]&0x20)==0x20)
                    {CySysCMD.bits.LEDWB3=1;}
                    else
                    {CySysCMD.bits.LEDWB3=0;}
                    //LEDWA3
                    if((msg_recive_buffer[7]&0x40)==0x40)
                    {CySysCMD.bits.LEDWA3=1;}
                    else
                    {CySysCMD.bits.LEDWA3=0;}
                }
                
            //App_UART_Send(msg_recive_buffer, 9);     
            //App_UART_Send(CySysCMD.mCMD, 7); 
        
          }
    }
}


/*******************************************************************************
* Function Name  : APP_receive_message_progress
* Description    : check uart message
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/

void APP_receive_message_progress(void)
{
    static uint8_t rx_index = 0;
    static uint8_t crc_data = 0;
	uint16_t rx_len = ring_buffer_num_items(&ring_buffer);
    if(rx_len == 0)
        return;
    if(rx_index == 0)
	{
		for(uint8_t i = 0; i < rx_len; i++)
		{
			char rxbyte1,rxbyte2;
			ring_buffer_dequeue(&ring_buffer, &rxbyte1);
			if((0x5A == rxbyte1))
			{
                ring_buffer_dequeue(&ring_buffer, &rxbyte2);
                if(rxbyte2==0xA5)
                {
				    msg_recive_buffer[0] = rxbyte1;
                    msg_recive_buffer[1] = rxbyte2;
				    rx_index+=2;
				    break;
                }
			}
		}
	}
	rx_len = ring_buffer_num_items(&ring_buffer);
	
	if(rx_len >= 7)   
	{
		rx_index += 7;
		ring_buffer_dequeue_arr(&ring_buffer, (char *)(msg_recive_buffer + 2), 7);
	    CySysFlag.bits.recive_msg_flag = TRUE;
        App_message_analyse();
		rx_index = 0;	
	}  
}
/*******************************************************************************
* Function Name  : App_protocol_send_live_singal
* Description    : live singal send 
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void App_protocol_send_live_singal(void)
{
	
//	uint8_t temp[7]={0};
//	temp[0]=protocol_live.fixed_id;
//	temp[1]=protocol_live.state_id;
//	temp[2]=protocol_live.touch_st;
//	temp[3]=protocol_live.live_si;
//	temp[4]=protocol_live.rst_num;
//	temp[5]=protocol_live.contact;
//	temp[6]=protocol_live.crc8;
//	App_UART_Send(temp, sizeof(temp));
	
}
/*******************************************************************************
* Function Name  : void App_send_touch_data(uint8_t finger_id_t,uint8_t flag_id,uint8_t xlsb,uint8_t xmsb,uint8_t ylsb,uint8_t ymsb )
* Description    : touch data send 
* Input          : finger_id_t:which finger  flag_id: up ,down or move  xlsb；x low cordination  xmsb:x high cordination  ylsb；y low cordination  ymsb:y high cordination
* Output         : None
* Return         : None
*******************************************************************************/

void App_send_touch_data(uint8_t finger_id_t,uint8_t flag_id,uint8_t xlsb,uint8_t xmsb,uint8_t ylsb,uint8_t ymsb )
{
			uint8_t crc_table_t[7] = {0};
			if(flag_id==0x02)
			{
				protocol_touch.state_id=(protocol_touch.state_id&0x0f)|0x20;
			}
			else if(flag_id==0x01)
			{
				protocol_touch.state_id=(protocol_touch.state_id&0x0f)|0x70;
			}
			else if(flag_id==0x03)
			{
				protocol_touch.state_id=(protocol_touch.state_id&0x0f)|0x60;
			}
			protocol_touch.fixed_id=0xA5;
			protocol_touch.state_id= (protocol_touch.state_id&0xf0)|finger_id_t;
			protocol_touch.xlsb = xlsb;
			protocol_touch.xmsb = xmsb;
			protocol_touch.ylsb = ylsb;
			protocol_touch.ymsb = ymsb;
			crc_table_t[0]=protocol_touch.fixed_id;
			crc_table_t[1]=protocol_touch.state_id;
			crc_table_t[2]=protocol_touch.xlsb;
			crc_table_t[3]=protocol_touch.xmsb;
			crc_table_t[4]=protocol_touch.ylsb;
			crc_table_t[5]=protocol_touch.ymsb;
			protocol_touch.crc8=App_CRC8_Check(crc_table_t,6);
            crc_table_t[6]=protocol_touch.crc8;
			App_UART_Send(crc_table_t, sizeof(crc_table_t));   
}










